package com.ruoyi.project.websiteContentTitle.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.project.websiteContent.domain.WebsiteContent;
import com.ruoyi.project.websiteContent.service.IWebsiteContentService;
import com.ruoyi.project.websiteContentTitle.domain.WebsiteTitle;
import com.ruoyi.project.websiteContentTitle.service.IWebsiteTitleService;
import com.ruoyi.project.websiteContentTitle.service.impl.WebsiteTitleServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 网站首页展示目录Controller
 * 
 * @author ruoyi
 * @date 2020-08-03
 */
@Controller
@RequestMapping("/system/title")
public class WebsiteTitleController extends BaseController
{
    private String prefix = "system/websiteContentTitle";

    @Autowired
    private IWebsiteTitleService websiteTitleService;

    @Autowired
    private IWebsiteContentService websiteContentService;

    @RequiresPermissions("system:title:view")
    @GetMapping()
    public String title()
    {
        return prefix + "/title";
    }

    /**
     * 查询网站首页展示目录树列表
     */
    @RequiresPermissions("system:title:list")
    @PostMapping("/list")
    @ResponseBody
    public List<WebsiteTitle> list(WebsiteTitle websiteTitle)
    {
        List<WebsiteTitle> list = websiteTitleService.selectWebsiteTitleList(websiteTitle);
        return list;
    }

    /**
     * 导出网站首页展示目录列表
     */
    @RequiresPermissions("system:title:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebsiteTitle websiteTitle)
    {
        List<WebsiteTitle> list = websiteTitleService.selectWebsiteTitleList(websiteTitle);
        ExcelUtil<WebsiteTitle> util = new ExcelUtil<WebsiteTitle>(WebsiteTitle.class);
        return util.exportExcel(list, "title");
    }

    /**
     * 新增网站首页展示目录
     */
    @GetMapping(value = { "/add/{titleId}", "/add/" })
    public String add(@PathVariable(value = "titleId", required = false) Long titleId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(titleId))
        {
            mmap.put("websiteTitle", websiteTitleService.selectWebsiteTitleById(titleId));
        }
        /**
         * 如果没有titleId 添加时默认在根目录下
         */
        if(StringUtils.isNull(titleId)){
            mmap.put("websiteTitle", websiteTitleService.selectWebsiteTitleById(1L));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存网站首页展示目录
     */
    @RequiresPermissions("system:title:add")
    @Log(title = "网站首页展示目录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebsiteTitle websiteTitle)
    {
        /**
         * 创建一个对象
         */
        WebsiteTitle websiteTitle1=new WebsiteTitle();
        websiteTitle1.setParentId(websiteTitle.getParentId());
        /**
         * 查询同一父级目录下的内容
         */
        List<WebsiteTitle> websiteTitles = websiteTitleService.selectWebsiteTitleList(websiteTitle1);
        boolean flag = false;
        for(WebsiteTitle title:websiteTitles){
            if(title.getTitleName().equals(websiteTitle.getTitleName())){
                return AjaxResult.error("请不要添加重复的名称");
            }
        }
        return toAjax(websiteTitleService.insertWebsiteTitle(websiteTitle));
    }

    /**
     * 修改网站首页展示目录
     */
    @GetMapping("/edit/{titleId}")
    public String edit(@PathVariable("titleId") Long titleId, ModelMap mmap)
    {
        WebsiteTitle websiteTitle = websiteTitleService.selectWebsiteTitleById(titleId);
        mmap.put("websiteTitle", websiteTitle);
        return prefix + "/edit";
    }

    /**
     * 修改保存网站首页展示目录
     */
    @RequiresPermissions("system:title:edit")
    @Log(title = "网站首页展示目录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebsiteTitle websiteTitle)
    {
        return toAjax(websiteTitleService.updateWebsiteTitle(websiteTitle));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:title:remove")
    @Log(title = "网站首页展示目录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{titleId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("titleId") Long titleId)
    {
        List<WebsiteTitle> list=websiteTitleService.getAllShowTwoTitle(titleId);
        WebsiteTitle websiteTitle=websiteTitleService.selectWebsiteTitleById(titleId);
        if(!StringUtils.isEmpty(list) || list.size()!=0){
            return AjaxResult.error("该目录下有二级分类,不可删除");
        }
        if(websiteTitle.getIfShow()==3){
            return AjaxResult.error("默认展示标题，不可删除");
        }
        int count=websiteTitleService.deleteWebsiteTitleById(titleId);
        if(count!=0){
            return AjaxResult.success("删除成功，同时删除标题下所有内容");
        } else{
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 选择网站首页展示目录树
     */
    @GetMapping(value = { "/selectTitleTree/{titleId}", "/selectTitleTree/" })
    public String selectTitleTree(@PathVariable(value = "titleId", required = false) Long titleId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(titleId))
        {
            mmap.put("websiteTitle", websiteTitleService.selectWebsiteTitleById(titleId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载网站首页展示目录树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = websiteTitleService.selectWebsiteTitleTree();
        return ztrees;
    }

    /**
     * 获取所有一级标题
     * @return
     */
    @GetMapping("/getAllShowTitle")
    @ResponseBody
    @CrossOrigin
    public AjaxResult getAllOneTitle(){
        List<WebsiteTitle> websiteTitles=websiteTitleService.getAllOneTitle();
        if(StringUtils.isNull(websiteTitles) || websiteTitles.size()==0){
            return AjaxResult.error("没有获取到标题数据");
        }
        return AjaxResult.success("获取到标题",websiteTitles);
    }

    /**
     * 获取二级目录
     * @param titleId
     * @return
     */
    @GetMapping("/getAllShowTwoTitle")
    @ResponseBody
    @CrossOrigin
    public AjaxResult getAllTwoTitle(Long titleId,Integer pageNum,Integer pageSize){
        /**
         * 获取一级标题下的二级标题操作
         */
        List<WebsiteTitle> websiteTitles=websiteTitleService.getAllShowTwoTitle(titleId);
        if(StringUtils.isNull(websiteTitles) || websiteTitles.size()==0){
            return AjaxResult.error("没有获取到标题数据");
        }
        List<WebsiteTitle> websiteTitles1=new ArrayList<>();
        for(int i=0;i<websiteTitles.size();i++){
            if(websiteTitles.get(i).getIfShow()!=2){
                websiteTitles1.add(websiteTitles.get(i));
            }
        }
        return AjaxResult.success("获取到内容",websiteTitles1);
    }


    /**
     * 根据id获取标题信息
     * @return
     */
    @GetMapping("/getTitleById")
    @ResponseBody
   public WebsiteTitle getWebSiteTitleById(Long titleId){
        return websiteTitleService.selectWebsiteTitleById(titleId);
   }
}
